package com.stx.jeepay.payment.task;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stx.jeepay.core.dao.PayOrderDivisionRecordDao;
import com.stx.jeepay.core.entity.PayOrderDivisionRecordEntity;
import com.stx.jeepay.core.entity.PayOrderEntity;
import com.stx.jeepay.core.service.PayOrderDivisionRecordService;
import com.stx.jeepay.core.service.PayOrderService;
import com.stx.jeepay.core.utils.SpringBeansUtil;
import com.stx.jeepay.payment.channel.IDivisionService;
import com.stx.jeepay.payment.model.MchAppConfigContext;
import com.stx.jeepay.payment.rqrs.msg.ChannelRetMsg;
import com.stx.jeepay.payment.service.ConfigContextQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 分账补单定时任务
 */
@Slf4j
@Component
public class PayOrderDivisionRecordReissueTask {

    private static final int QUERY_PAGE_SIZE = 100; //每次查询数量

    @Autowired
    private
    PayOrderDivisionRecordService payOrderDivisionRecordService;
    @Autowired
    private ConfigContextQueryService configContextQueryService;
    @Autowired
    private PayOrderService payOrderService;
    @Autowired
    private PayOrderDivisionRecordDao payOrderDivisionRecordDao;

    @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次
    public void start() {
        log.info("处理分账补单任务 开始时间:" + new Date());

        // 当前时间 减去五分钟
        Date offsetDate = DateUtil.offsetMinute(new Date(), -4);

        //查询条件： 受理中的订单 & （ 订单创建时间 + 5分钟 >= 当前时间 ）
        LambdaQueryWrapper<PayOrderDivisionRecordEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PayOrderDivisionRecordEntity::getState, PayOrderDivisionRecordEntity.STATE_ACCEPT);
        queryWrapper.le(PayOrderDivisionRecordEntity::getCreatedAt, offsetDate);

        int currentPageIndex = 1; //当前页码

        while (true) {
            try {
                IPage<PayOrderDivisionRecordEntity> pageRecordList =
                        payOrderDivisionRecordDao.distinctBatchOrderIdList(new Page(currentPageIndex, QUERY_PAGE_SIZE), queryWrapper);

                // 如果没查到数据就结束
                if (pageRecordList == null || pageRecordList.getRecords() == null || pageRecordList.getRecords().isEmpty()) {
                    break;
                }

                for (PayOrderDivisionRecordEntity batchRecord : pageRecordList.getRecords()) {
                    try {
                        String batchOrderId = batchRecord.getBatchOrderId();
                        // 通过batchId查询List
                        List<PayOrderDivisionRecordEntity> recordList = payOrderDivisionRecordService.list(new LambdaQueryWrapper<PayOrderDivisionRecordEntity>()
                                .eq(PayOrderDivisionRecordEntity::getState, PayOrderDivisionRecordEntity.STATE_ACCEPT)
                                .eq(PayOrderDivisionRecordEntity::getBatchOrderId, batchOrderId)
                                .orderByDesc(PayOrderDivisionRecordEntity::getRecordId));

                        if (CollUtil.isEmpty(recordList)) {
                            continue;
                        }

                        //查询支付订单信息
                        PayOrderEntity payOrder = payOrderService.getById(batchRecord.getPayOrderId());
                        if (payOrder == null) {
                            log.error("支付订单记录不存在：{}", batchRecord.getPayOrderId());
                            continue;
                        }

                        // 查询转账接口是否存在
                        IDivisionService divisionService =
                                SpringBeansUtil.getBean(payOrder.getIfCode() + "DivisionService", IDivisionService.class);
                        if (divisionService == null) {
                            log.error("查询分账接口不存在：{}", payOrder.getIfCode());
                            continue;
                        }

                        MchAppConfigContext mchAppConfigContext =
                                configContextQueryService.queryMchInfoAndAppInfo(payOrder.getMchNo(), payOrder.getAppId());
                        // 调用渠道侧的查单接口：   注意：  渠道内需保证：
                        // 1. 返回的条目 必须全部来自recordList， 可以少于recordList但是不得高于 recordList 数量；
                        // 2. recordList 的记录可能与接口返回的数量不一致，  接口实现不要求对条目数量做验证；
                        // 3. 接口查询的记录若recordList 不存在， 忽略即可。  （  例如两条相同的accNo, 则可能仅匹配一条。 那么另外一条将在下一次循环中处理。  ）
                        // 4. 仅明确状态的再返回，若不明确则不需返回；
                        HashMap<Long, ChannelRetMsg> queryDivision =
                                divisionService.queryDivision(payOrder, recordList, mchAppConfigContext);

                        // 处理查询结果
                        for (PayOrderDivisionRecordEntity record : recordList) {
                            ChannelRetMsg channelRetMsg = queryDivision.get(record.getRecordId());

                            // 响应状态为分账成功或失败时，更新该记录状态
                            if (ChannelRetMsg.ChannelState.CONFIRM_SUCCESS == channelRetMsg.getChannelState()
                                    || ChannelRetMsg.ChannelState.CONFIRM_FAIL == channelRetMsg.getChannelState()) {
                                Byte state = ChannelRetMsg.ChannelState.CONFIRM_SUCCESS == channelRetMsg.getChannelState() ? PayOrderDivisionRecordEntity.STATE_SUCCESS : PayOrderDivisionRecordEntity.STATE_FAIL;
                                // 更新数据库中记录的状态
                                payOrderDivisionRecordService.updateRecordSuccessOrFailBySingleItem(record.getRecordId(), state, channelRetMsg.getChannelErrMsg());
                            }
                        }

                    } catch (Exception e1) {
                        log.error("处理补单任务单条[{}]异常", batchRecord.getBatchOrderId(), e1);
                    }
                }

                //已经到达页码最大量，无需再次查询
                if (pageRecordList.getPages() <= currentPageIndex) {
                    break;
                }
                currentPageIndex++;
            } catch (Exception e) {
                //出现异常，直接退出，避免死循环。
                log.error("处理分账补单任务, error", e);
                break;
            }
        }
    }
}
